<template>
	<view class="order-detail">
		<!-- status -->
		<view class="order-detail__status">
			<view class="status">{{ replaceStatus(pageData.status) }}</view>
			<!-- <view class="info" v-if="status == 1">预计7月3日（周六）11:00送达</view> -->
			<!-- <view class="info" v-if="status == 0">剩余支付时间：29:23:24</view> -->
		</view>

		<!-- 地址 -->
		<view class="order-detail__box">
			<view class="order-detail__selected">
				<view class="order-detail__selected__top">{{ pageData.name }} {{ pageData.phone }}</view>
				<view class="order-detail__selected__bot">{{ pageData.address }}</view>
			</view>
		</view>

		<!-- 商品明细 -->
		<view class="order-detail__box">
			<view class="goods-list__box" v-for="(item, index) in pageData.details" :key="index">
				<image class="goods-list__box__image" :src="item.rec_pic" mode="widthFix"></image>
				<view class="goods-list__box__right">
					<view class="goods-list__box__top">
						<view class="goods-list__box__title">{{ item.name }}</view>
					</view>
					<view v-if="item.unit_price" class="goods-list__box__bot">¥{{ item.unit_price.toFixed(2) }}/件
						共{{item.count}}件</view>
				</view>
				<view class="price">
					<span>¥</span>{{ (pageData.status == 0 || pageData.status == 1) ? item.unit_price.toFixed(2) : item.disparity.toFixed(2)  }}
				</view>
				<view class="weight" v-if="pageData.status != 0 && pageData.status != 1">共{{ item.sortingWeight }}斤
				</view>
				<!-- v-if="pageData.status == 2" -->
				<view class="btn"
					v-if="(pageData.status == 2 || pageData.status == 4 || pageData.status == 5 || pageData.status == 6 || pageData.status == 7) && item.status === '0'"
					@click=" goReport(item)">售后</view>
				<view class="btn" style="border: none;" v-if="item.status === '4' || item.status === '5'">售后中</view>
				<view class="btn" style="border: none;" v-if="item.status === '6'">已拒绝</view>
				<view class="btn" style="border: none;" v-if="item.status === '7'">退款成功</view>
			</view>
			<!-- 超出3种商品 -->
			<!-- <view v-if="pageData.details.length > 3" class="order-detail__over" @click="showMore = !showMore">
				{{ showMore ? '收起' : '展开'}}(共{{ pageData.count }}件)
				<image class="order-detail__over__icon" :class="showMore ? 'tr2' : 'tr'" mode="widthFix"
					:src="$imgUrl('/order/createorder/icon-right.png')"></image>
			</view> -->
			<!-- 价格 -->
			<view class="order-detail__price">
				<view class="order-detail__price__line1">
					<view class="unit">商品总额</view>
					<view class="amount">
						<span>￥</span>{{ (pageData.status == 0 || pageData.status == 1) ? pageData.amount.toFixed(2) : pageData.disparity.toFixed(2) }}
					</view>
				</view>
				<view class="order-detail__price__line2">
					<span class="text">实付合计：</span>
					<span class="unit">¥</span>
					{{ pageData.amount.toFixed(2) }}
				</view>
			</view>
		</view>

		<!-- 异常金额 -->
		<view class="order-detail__box" v-if="pageData.status != 0 && pageData.status != 1">
			<view class="order-detail__box__title">异常支付</view>
			<view class="order-detail__box__orderInfo">
				<view class="label">余额支付</view>
				<view class="info">{{ stPrice }}</view>
			</view>
		</view>

		<!-- 订单金额价格 -->
		<view class="order-detail__box">
			<view class="order-detail__box__title">订单信息</view>
			<view class="order-detail__box__orderInfo">
				<view class="label">订单编号</view>
				<view class="info">{{ pageData.code }}</view>
				<view class="copy" @click="copyCode">复制</view>
			</view>
			<view class="order-detail__box__orderInfo">
				<view class="label">下单时间</view>
				<view class="info">{{ pageData.create_time }}</view>
			</view>
			<view class="order-detail__box__orderInfo">
				<view class="label">支付方式</view>
				<view class="info">微信支付</view>
			</view>
		</view>

		<!-- 固底提交按钮 -->
		<view class="order-detail__btn">
			<view class="btn color1" v-if="pageData.status == 0">联系客服</view>
			<view class="btn color1" v-if="pageData.status == 1 || pageData.status == 0">取消订单</view>
			<view class="btn color2" v-if="pageData.status == 0" @click="toPay">立即支付</view>
			<view class="btn color2" @click="confirmOrder"
				v-if="pageData.status == 2 || pageData.status == 5 || pageData.status == 4 || pageData.status == 6 || pageData.status == 7 ">
				确认收货
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	import {
		accSub
	} from '@/utils/index.js'
	export default {
		data() {
			return {
				pageData: {
					status: null,
					details: [],
					amount: 0
				},
				showMore: false,
				id: '',
			}
		},
		computed: {
			stDetails() {
				// return this.pageData.details
				if (this.showMore) {
					return this.pageData.details
				} else {
					return this.pageData.details.slice(0, 3)
				}
			},
			stPrice() {
				return accSub(this.pageData.disparity, this.pageData.amount)
			}
		},
		onLoad(options) {
			this.id = options.id

		},
		onShow() {
			this.getOrderDetail(this.id)
		},
		methods: {
			replaceStatus(status) {
				switch (status) { // 状态 0 待支付 1 待发货 2 待收货 3 售后
					case '0':
						return '待支付'
						break;
					case '1':
						return '商品待分拣'
						break;
					case '2':
						return '商品已送达'
						break;
					case '3':
						return '待收货'
						break;
					default:
						break;
				}
			},
			async getOrderDetail(id) {
				const data = await this.$apis.order.getOrderDetail({
					id
				})
				this.pageData = data.data
			},
			// 确认收货
			async confirmOrder() {
				uni.showLoading({
					mask: true
				})
				const res = await this.$apis.order.confirmOrder({
					orderId: this.id
				})
				uni.hideLoading()
				this.getOrderDetail(this.pageData.id)
			},
			async toPay() {
				uni.showLoading({
					mask: true
				})
				// 创建成功
				const res = await this.$apis.order.getWxpay({
					code: this.pageData.code,
					payType: 0,
				})
				uni.requestPayment({
					provider: "wxpay",
					nonceStr: res.data.nonceStr,
					package: res.data.package,
					timeStamp: res.data.timeStamp,
					paySign: res.data.sign,
					signType: res.data.signType,
					success(res) {
						uni.hideLoading()
						uni.navigateTo({
							url: '/pages/order/paySuccess/index'
						})
					},
					fail(e) {
						uni.hideLoading()
					}
				})
			},
			goReport(item) {
				uni.navigateTo({
					url: '/pages/report/report?item=' + encodeURIComponent(JSON.stringify(item))
				})
			},
			copyCode() {
				uni.setClipboardData({
					data: this.pageData.code,
					success: function() {
						console.log('success');
						uni.showToast({
							title: '复制成功',
							icon: 'none',
							duration: 2000
						});
					}
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	.order-detail {
		min-height: 100vh;
		padding: 20rpx;
		padding-bottom: 98rpx;
		background: #F6F6F6;

		.order-detail__status {
			padding: 24rpx 0;
			padding-left: 12rpx;

			.status {
				font-size: 36rpx;
				font-weight: bold;
				color: #333333;
				line-height: 50rpx;
			}

			.info {
				font-size: 28rpx;
				color: #333333;
				line-height: 40rpx;
				margin-top: 8rpx;
			}
		}

		.order-detail__box {
			padding: 24rpx;
			background: #FFFFFF;
			border-radius: 16rpx;
			position: relative;
			margin-bottom: 20rpx;

			.order-detail__box__title {
				padding-bottom: 24rpx;
				font-size: 30rpx;
				font-weight: bold;
				color: #333333;
				border-bottom: 1rpx solid #E2E2E2;
			}
		}

		.order-detail__selected__top {
			font-size: 32rpx;
			font-weight: bold;
			color: #333333;
			line-height: 45rpx;
			margin-bottom: 16rpx;
		}

		.order-detail__selected__bot {
			font-size: 28rpx;
			color: #666666;
			line-height: 40rpx;
		}

		.goods-list__box {
			width: 100%;
			height: 168rpx;
			margin-top: 24rpx;
			display: flex;
			position: relative;

			.price {
				position: absolute;
				top: 0;
				right: 0;
				font-size: 28rpx;
				font-weight: bold;
				color: #333333;

				span {
					font-size: 22rpx;
				}
			}

			.weight {
				position: absolute;
				top: 44rpx;
				right: 0;
				font-size: 22rpx;
				color: #999999;
			}

			.btn {
				width: 120rpx;
				height: 44rpx;
				background: #FFFFFF;
				border-radius: 22rpx;
				border: 1rpx solid #E2E2E2;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 24rpx;
				color: #666666;
				position: absolute;
				right: 0;
				bottom: 22rpx;
			}

			.goods-list__box__image {
				width: 148rpx;
				height: 148rpx;
				border-radius: 8rpx;
				margin-right: 16rpx;
			}

			.goods-list__box__right {
				width: 330rpx;
				height: 148rpx;
				padding-bottom: 24rpx;
				display: flex;
				flex-direction: column;
			}

			.goods-list__box__title {
				font-size: 28rpx;
				color: #333333;
				line-height: 40rpx;
				width: 100%;
				word-break: break-all;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
				overflow: hidden;
			}

			.goods-list__box__bot {
				font-size: 22rpx;
				color: #999999;
				margin-top: 15rpx;
			}

		}

		.order-detail__over {
			margin-top: 12rpx;
			margin-bottom: 12rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 28rpx;
			color: #999999;

			.order-detail__over__icon {
				width: 44rpx;
				height: 44rpx;
				margin-left: 16rpx;

				&.tr {
					transform: rotate(90deg);
				}

				&.tr2 {
					transform: rotate(-90deg);
				}
			}
		}

		.order-detail__box__orderInfo {
			margin-top: 24rpx;
			display: flex;
			align-items: center;

			.label {
				font-size: 28rpx;
				color: #999999;
				line-height: 40rpx;
				margin-right: 24rpx;
			}

			.info {
				font-size: 28rpx;
				color: #333333;
				line-height: 40rpx;
			}

			.copy {
				width: 64rpx;
				height: 40rpx;
				background: #F0F0F0;
				border-radius: 6rpx;
				font-size: 24rpx;
				color: #333333;
				display: flex;
				align-items: center;
				justify-content: center;
				margin-left: 24rpx;
			}
		}

		.order-detail__btn {
			position: fixed;
			left: 0;
			bottom: 0;
			z-index: 5;
			width: 706rpx;
			height: 96rpx;
			padding: 0 20rpx 0 24rpx;
			background: #FFFFFF;
			display: flex;
			align-items: center;
			justify-content: flex-end;

			.btn {
				width: 160rpx;
				height: 64rpx;
				border-radius: 32rpx;
				margin-left: 24rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 28rpx;

				&.color1 {
					background: #FFFFFF;
					border: 1rpx solid #CCCCCC;
					color: #666666;
				}

				&.color2 {
					background: #2FCC71;
					color: #FFFFFF;
				}
			}

		}

		.order-detail__price {
			padding-top: 29rpx;
			padding-bottom: 32rpx;
			border-top: 1rpx solid #E2E2E2;

			.order-detail__price__line1 {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.unit {
					font-size: 24rpx;
					font-weight: bold;
					color: #666666;
				}

				.amount {
					font-size: 28rpx;
					font-weight: bold;
					color: #333333;

					span {
						font-size: 22rpx;
					}
				}
			}

			.order-detail__price__line2 {
				display: flex;
				align-items: center;
				justify-content: flex-end;
				font-weight: bold;
				color: #333333;
				font-size: 34rpx;

				.text {
					font-size: 24rpx;
				}

				.unit {
					font-size: 28rpx;
				}

			}
		}
	}
</style>